﻿using DevExpress.XtraEditors;
using DevTeam;
using System;
using System.Data;

namespace ConfExpert.Forms.ImportWizards.Kargo
{
    public partial class KargoFatura : XImportWizard
    {
        public KargoFatura()
        {
            InitializeComponent();

            Init("Kargo Faturası", false);

            DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit riKargoFirm = new DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit();
            riKargoFirm.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            DataTable dtKargo = Session.DataManager.ExecuteDataTableByQuery("select * from KargoFirma");
            riKargoFirm = GetCustomGLE(dtKargo, "Name", "KargoFirmaId");
            gleKargoFirma.Edit.Assign(riKargoFirm);
            gleKargoFirma.EditValue = 1;

            btnSave.ItemClick += BtnSave_ItemClick;

            this.FileDone += KargoFatura_FileDone;
            this.ProcessStart += KargoFatura_ProcessStart;
            this.ProcessDone += KargoFatura_ProcessDone;
        }

        private void BtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int kargoFirmaId = gleKargoFirma.EditValue.ToInt(-1);
            if (kargoFirmaId < 1)
            {
                XtraMessageBox.Show("Lütfen kargo firmasını seçiniz!");
                return;
            }
            else
            {
                int currencyId = new int();
                switch (kargoFirmaId)
                {
                    case 1: currencyId = 1; break;
                    case 2: currencyId = 2; break;
                    default: break;
                }

                //TODO: tek seferde gönderip insert edelim (hatalara karşı da önceden uyarmak lazım, transaction'ı bölmek iyi fikir değil!)
                foreach (DataRow dr in dtResult.Rows)
                {
                    string wayBillNo = dr["AWB"].ToStr();
                    string receiver = dr["ALICI"].ToString();
                    string gonderen = dr["GND"].ToString();
                    DateTime shipmentDate = dr["GNDTRH"].ToDate();
                    int pieces = dr["ADET"].ToDecimal(-1).ToInt();
                    decimal invAmount = dr["NAVLUN"].ToDecimal(-1);
                    decimal agirlik = dr["KG"].ToDecimal(-1);

                    ExecuteNonQuery("InsKargoRapor", kargoFirmaId, wayBillNo, receiver, gonderen, shipmentDate, pieces, invAmount, agirlik, currencyId);
                }
            }
            Notify("Aktarım başarılı.");
        }

        DataTable dtResult;

        void KargoFatura_ProcessStart()
        {
            dtResult = new DataTable();
            dtResult.TableName = "Fatura";

            DataSet dsResult = new DataSet();
            dsResult.Tables.Add(dtResult);
        }

        void KargoFatura_ProcessDone()
        {
            MasterGrid.Init(dtResult);
            MasterGrid.AddSummary();
            MasterGrid.BestFitColumns();

            btnSave.Enabled = true;
        }

        void KargoFatura_FileDone(DataSet ds, string FileName)
        {
            DataTable dt = ds.Tables[0];    //"KARBEL" isimli çoğu zaman
            DataTable dtSheet = ProcessSheet(dt);

            dtSheet.Columns.Add("DosyaAdi", typeof(string));

            foreach (DataRow dr in dtSheet.Rows)
                dr["DosyaAdi"] = FileName;  //*

            dtResult.Merge(dtSheet);
        }

        private DataTable ProcessSheet(DataTable dt)
        {
            DataTable dtKargo = new DataTable();
            DataRow drCaption = dt.Rows[0];

            for (int i = 0; i < dt.Columns.Count; i++) // Başlıkları set ediyoruz.
            {
                dtKargo.Columns.Add();
                dtKargo.Columns[i].ColumnName = drCaption[i].ToStr();
            }

            for (int j = 1; j < dt.Rows.Count; j++)
                if (dt.Rows[j].Field<string>(0) != null && dt.Rows[j].Field<string>(3) != null)
                    dtKargo.Rows.Add(dt.Rows[j].ItemArray);

            foreach (DataRow dr in dtKargo.Rows)
                dr["GNDTRH"] = DateTime.FromOADate(double.Parse(dr["GNDTRH"].ToString())).ToString("dd.MM.yyyy");

            return dtKargo;
        }

        private void KargoFatura_Load(object sender, EventArgs e)
        {

        }
    }
}